package com.penton.util.http;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * 主要是采用HttpClient框架请求webservice<br/>
 * 其中包括Get、post请求和图片下载
 * @author pengjianbo
 *
 */
public enum  HttpRequest {
	Instance;
	
	private DefaultHttpClient mHttpClient;
	
	private static final int TIMEOUT = 20;
	
	private static final int BUFF_SIZE = 8192;
	
	/**
	 * 构造函数，只执行一次
	 */
	HttpRequest(){
		HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setStaleCheckingEnabled(params, false);
        HttpConnectionParams.setConnectionTimeout(params, TIMEOUT * 1000);
        HttpConnectionParams.setSoTimeout(params, TIMEOUT * 1000);
        HttpConnectionParams.setSocketBufferSize(params, BUFF_SIZE);
        
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register( new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);

        /*
         * 添加WAP支持
        HttpHost proxy = new HttpHost("10.0.0.172", 80);
        mHttpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
        */
        mHttpClient = new DefaultHttpClient(cm, params);
	}
	
	/**
	 * GET方法
	 * @param url
	 * @return 返回http请求的正文
	 */
	public byte[] get(String url){
		
		if ( url.trim().equals("") ) {
			return null;
		}
		
		byte[] bytes = null;
		
		try {
			HttpGet httpGet 		= new HttpGet(url);
			HttpResponse response 	= mHttpClient.execute(httpGet);
        	HttpEntity entity 		= response.getEntity();

        	if (entity != null) {
				InputStream inputStream = null;
				try {
					inputStream = entity.getContent();
					
					ByteArrayOutputStream bos = new ByteArrayOutputStream();  
					
					int ch; 
					byte[] buf = new byte[BUFF_SIZE]; 
					while ( (ch = inputStream.read(buf)) != -1 ) {  
						bos.write(buf, 0, ch); 
					}  
					
					bytes = bos.toByteArray();  
					bos.close(); 
					inputStream.close();
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				entity.consumeContent();
        	}
			
		} catch ( Exception e ) {
			e.printStackTrace();
		} finally {
			//mHttpClient.getConnectionManager().releaseConnection(null, (Long) null, null);
			//httpClient.getConnectionManager().shutdown();
        }
		
		return bytes;
		
	}
	
	/**
	 * 此方法实现http的POST请求
	 * @param url
	 * @param postField
	 * @param localContext
	 * @return
	 */
	public byte[] post(String url, HashMap<String, Object> postField) {
		
		byte[] bytes = null;
		
        HttpPost httpPost = new HttpPost(url);
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "UTF-8", null);
        
        
        for (Iterator<Entry<String, Object>> it = postField.entrySet().iterator(); it.hasNext(); ) {
        	
			Map.Entry<String, Object> e = (Map.Entry<String, Object>)it.next();
			String key = (String) e.getKey();
			Object value = e.getValue();
			
			if ( value == null ) {
			} else {
				
				if (value instanceof ByteArrayBody){
					reqEntity.addPart(key, (ByteArrayBody) value);
				} else if(value instanceof FileBody) {
					reqEntity.addPart(key, (FileBody) value);
					
				} else if(value instanceof InputStreamBody) {
					reqEntity.addPart(key, (InputStreamBody) value ); 
				} else if(value instanceof String) {
					
					try {
						StringBody strField = new StringBody( value.toString(), Charset.forName(HTTP.UTF_8) );
						reqEntity.addPart(key, strField);
					} catch (UnsupportedEncodingException e1) {
						e1.printStackTrace();
					}
					
				} else {
					try {
						StringBody strField = new StringBody( value.toString(), Charset.forName(HTTP.UTF_8) );
						reqEntity.addPart(key, strField);
					} catch (UnsupportedEncodingException e1) {
						e1.printStackTrace();
					}
					
				}
			}
			
        }
        
    
        httpPost.setEntity(reqEntity);
        
		// 设置cookie的兼容性，这一行必须要加，否则服务器无法获取登陆状态
		HttpClientParams.setCookiePolicy(mHttpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);
		
		try {
			HttpResponse response 	= mHttpClient.execute(httpPost);
			HttpEntity entity 		= response.getEntity();
			
			if ( entity != null ){
				InputStream is = null;
				try {
					is = entity.getContent();
					ByteArrayOutputStream bos = new ByteArrayOutputStream();  
					
					int ch; 
					byte[] buf = new byte[BUFF_SIZE]; 
					while ( (ch = is.read(buf)) != -1 ) {  
						bos.write(buf, 0, ch); 
					} 
					
					bytes = bos.toByteArray();  
					bos.close();
					is.close();
				} catch ( Exception e){
					e.printStackTrace();
				}
				
				entity.consumeContent();
			}
        	
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
        }
        
		
		return bytes;
	}

	/**
	 * 从网络下载图片
	 * @param imageUrl
	 * @return
	 */
	public byte[] downloadImage(String url){
		if ( url.trim().equals("") ) {
			return null;
		}
		
		byte[] bytes = null;
		
		try {
			HttpGet httpGet 		= new HttpGet(url);
			HttpResponse response 	= mHttpClient.execute(httpGet);
        	HttpEntity entity 		= response.getEntity();

        	if (entity != null) {
				InputStream inputStream = null;
				try {
					inputStream = entity.getContent();
					
					final BitmapFactory.Options options = new BitmapFactory.Options();
					final Bitmap bmp = BitmapFactory.decodeStream(inputStream,
	                    null, options);
	
					ByteArrayOutputStream convertStream = new ByteArrayOutputStream(
	            		bmp.getWidth() * bmp.getHeight() * 4);
					bmp.compress(Bitmap.CompressFormat.JPEG, 95, convertStream);
					convertStream.flush();
					convertStream.close();
	            
					bmp.recycle();
					bytes = convertStream.toByteArray();
					
					inputStream.close();

				}catch ( Exception e ) {
					e.printStackTrace();
				}
				
				entity.consumeContent();
        	}
		}catch ( Exception e ) {
			e.printStackTrace();
		} finally {
			
		}
		
		return bytes;
		
	}
	
}